package com.ruiben.mall.controller;

import com.ruiben.mall.utils.CommonJSONResult;
import com.ruiben.mall.utils.DateUtil;
import com.ruiben.mall.utils.FileUploadUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.ruiben.mall.config.Config.BASE_URL;

@RestController
@RequestMapping("upload")
public class UploadController {

    @Value("${file_space}")
    private String fileSpace;

    @PostMapping(value = "uploadBannerImg", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public CommonJSONResult uploadBannerImg(MultipartFile file) {

        // 保存到数据库中的相对路径
        String uploadPathDB = "/banner";

        // 文件上传的最终保存路径
        String finalFacePath = fileSpace + uploadPathDB;
        String s = FileUploadUtils.saveImg(file, finalFacePath);
        if (StringUtils.isBlank(s)) {
            return CommonJSONResult.errorMsg("上传失败");
        } else {
            uploadPathDB += ("/" + s);
            return CommonJSONResult.ok(BASE_URL + uploadPathDB);

        }
    }

    @PostMapping(value = "uploadGoodsImg", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public CommonJSONResult uploadGoodsImg(MultipartFile file) {

        String yyyyMMdd = DateUtil.timeStamp2Date(DateUtil.timeStamp(), "yyyyMMdd");
        // 保存到数据库中的相对路径
        String uploadPathDB = File.separator + "product" + File.separator + yyyyMMdd + File.separator;

        // 文件上传的最终保存路径
        String finalFacePath = fileSpace + uploadPathDB;
        String s = FileUploadUtils.saveImg(file, finalFacePath);
        if (StringUtils.isBlank(s)) {
            return CommonJSONResult.errorMsg("上传失败");
        } else {
            uploadPathDB +=  s;
            return CommonJSONResult.ok(BASE_URL + uploadPathDB);

        }
    }


    @PostMapping(value = "uploadCommentImg", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public CommonJSONResult uploadCommentsImg(@RequestParam("file") MultipartFile[] files) {

      if (files!=null){
          List<String> images = new ArrayList<>();
          for (MultipartFile file:files){
              String yyyyMMdd = DateUtil.timeStamp2Date(DateUtil.timeStamp(), "yyyyMMdd");
              // 保存到数据库中的相对路径
              String uploadPathDB = File.separator + "comment" + File.separator + yyyyMMdd + File.separator;

              // 文件上传的最终保存路径
              String finalFacePath = fileSpace + uploadPathDB;
              String s = FileUploadUtils.saveImg(file, finalFacePath);
              uploadPathDB +=  s;
              images.add(uploadPathDB);
          }
          return CommonJSONResult.ok(images);

      }
      return CommonJSONResult.errorMsg("上传失败");
//
//        String yyyyMMdd = DateUtil.timeStamp2Date(DateUtil.timeStamp(), "yyyyMMdd");
//        // 保存到数据库中的相对路径
//        String uploadPathDB = File.separator + "comment" + File.separator + yyyyMMdd + File.separator;
//
//        // 文件上传的最终保存路径
//        String finalFacePath = fileSpace + uploadPathDB;
//        String s = FileUploadUtils.saveImg(file, finalFacePath);
//        if (StringUtils.isBlank(s)) {
//            return CommonJSONResult.errorMsg("上传失败");
//        } else {
//            uploadPathDB +=  s;
//            return CommonJSONResult.ok(BASE_URL + uploadPathDB);
//
//        }
//        return CommonJSONResult.ok(BASE_URL);
    }


    @PostMapping(value = "uploadReturnGoodsImg", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public CommonJSONResult uploadReturnGoodsImg(MultipartFile file) {

        String yyyyMMdd = DateUtil.timeStamp2Date(DateUtil.timeStamp(), "yyyyMMdd");
        // 保存到数据库中的相对路径
        String uploadPathDB = File.separator + "returngoods" + File.separator + yyyyMMdd + File.separator;

        // 文件上传的最终保存路径
        String finalFacePath = fileSpace + uploadPathDB;
        String s = FileUploadUtils.saveImg(file, finalFacePath);
        if (StringUtils.isBlank(s)) {
            return CommonJSONResult.errorMsg("上传失败");
        } else {
            uploadPathDB +=  s;
            return CommonJSONResult.ok(BASE_URL + uploadPathDB);

        }
    }

}
